﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using ScFramework.Core;
using System;

public class LocalServer  :SystemModule
{
    public override void Enable()
    {
        base.Enable();
        _main.Get<MessageManager>().Get<LocalServerEntityDataEvent>().RegisterHandler(LocalServerEntityDataEvent.FromPhysics, ReceieveFromPhysics);
        _main.Get<MessageManager>().Get<LocalServerEntityDataEvent>().RegisterHandler(LocalServerEntityDataEvent.FromLogics, ReceieveFromLogic);
 
    }
    #region ShareData
    ISharedEntityDataManager sharedEntityDataManager = null;
    public void InitData(INeedEntityData i)
    {
        if( sharedEntityDataManager != null)
        {
            i.InitData(sharedEntityDataManager.GetAllData());

        }else
        {
            Debug.LogWarning($"{sharedEntityDataManager} is not exist");
        }
    }
    public void RegisterDataManager(ISharedEntityDataManager m)
    {
        sharedEntityDataManager = m;
    }
    #endregion
    EventArgs ReceieveFromLogic(object sender, EventArgs args)
    {
        if (args is EntityDataReqArgs e)
        {
            if (_main.Get<UserManager>().IsHost)
            {
                _main.Get<MessageManager>().Get<LocalServerEntityDataEvent>().DispatchMessage(LocalServerEntityDataEvent.ToPhysics, this, e);
                //dispath message to all
            }
            else
            {
                //dispath message to host
            }
        }
        else throw new Exception();
        return null;
    }
    EventArgs ReceieveFromPhysics(object sender, EventArgs args)
    {
        if (args is EntityDataReqArgs e)
        {
            if (_main.Get<UserManager>().IsHost)
            {
                //dispath message to guest
            }
            else
            {
                //dispath message to host
            }
        }
        else if(args is EntityDataLocalArgs a)
        {
            _main.Get<MessageManager>().Get<LocalServerEntityDataEvent>().DispatchMessage(LocalServerEntityDataEvent.ToLogics,this, a);
        }
        else throw new Exception();
        return null;
    }
}
public enum LocalServerEntityDataEvent
{
    ToPhysics,
    ToLogics,
    FromLogics,
    FromPhysics,

}
public class EntityDataReqArgs : EventArgs
{
    public List<EntityChangeArgs> messages;
}
public class EntityDataLocalArgs : EventArgs
{
    public List<EntityChangeArgs> messages;
}